<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../rurple.css" type="text/css" />

<title>Chocolate Mousse Cake Recipe</title>
</head>
<body>
<h2 class="title">34. Chocolate mousse cake recipe</h2>

<p>I don't know about you, but I'm getting hungry.  It would be so nice to have some delicious chocolate mousse cake to eat.  We need:</p>

<ul>
<li><span style="text-decoration: underline;">List of ingredients</span></li>
<ul>
<li>4 eggs</li>
<li>100 g of sugar</li>
<li>250 g of dark semi-sweet chocolate</li>
<li>30 ml of cocoa powder</li>
</ul>
</ul>

<p>To actually make the cake, we need the recipe.  Something like:</p>

<ul>
<li>Preheat the oven to 180 C (350 F).</li>
<li>Separate the egg whites from the yolks.</li>
<li>etc.</li>
</ul>

<p>A recipe is very much like a computer program: it's a series of instructions that one needs to follow in a precise order to accomplish a desired result.  I think we have enough to learn about programming, that I will end the cooking lesson at this point and go back to Python.</p>

<!--==============================-->
<hr class="line" />

<h3 class="section">Lists</h3>

<p>Here's a little something for you to try with the Python interpreter:</p>

<pre>
>>> ingredients = [<span class="string">"4 eggs"</span>, <span class="string">"100 g of sugar"</span>, 
...     <span class="string">"250 g of dark semi-sweet chocolate"</span>, <span class="string">"30 ml of cocoa powder"</span>]
>>> 
</pre>

<p><tt>ingredients</tt> is an example of a Python <i>list</i>.  A list in Python can include any number of items.  It the example we have chosen, all the ingredients are strings; however, list <i>items</i> could be numbers, or even other lists and other Python objects that we have yet to see.  Now, try the following:</p>

<pre>
>>> <span class="keyword">for</span> each_item <span class=
"keyword">in</span> ingredients:
...    <span class="keyword">print</span> each_item
4 eggs
100 g of sugar
250 g of dark semi-sweet chocolate
30 ml of cocoa powder

>>> ingredients[0]
<span class="string">'4 eggs'</span>

>>> ingredients[0:2]
[<span class="string">'4 eggs'</span>, <span class="string">'100 g of sugar'</span>]
</pre>

<p>It should look familiar, yet slightly different from what we have seen before. Try also the following:</p>

<pre>
>>> <span class="keyword">for</span> character <span class="keyword">in</span> ingredients[0]:
...     <span class="keyword">print</span> character
...    
4
 
e
g
g
s

>>> ingredients[0][2]
<span class="string">'e'</span>

>>> <span class="string">'4 eggs'</span>[2]
<span class="string">'e'</span>
</pre>

<p>As we had just seen previously, <tt>ingredients[0]</tt> is a synonym for the string <span class="string">'4 eggs'</span>; so <tt>ingredients[0][2]</tt> is the third character (remember Python starts counting at zero) in the string <span class="string">'4 eggs'</span>.</p>

<p>Now, in the recipe above, I forgot one nice ingredient: <i>whipped cream</i> to be used as a topping.  I can add it, and verify that it's been added as follows:</p>

<pre>
>>> ingredients.append(<span class="string">"whipped cream"</span>)
>>> <span class="keyword">print</span> ingredients
[<span class="string">'4 eggs'</span>, <span class="string">'100 g of sugar'</span>, <span class="string">'250 g of dark semi-sweet chocolate'</span>, <span class="string">'30 ml of cocoa powder'</span>, <span class="string">'whipped cream'</span>]
</pre>

<p><tt>append()</tt> is a method that is part of the class "list".  <tt>ingredients</tt> is an instance of that class.  We <i>invoke</i> the method <tt>append</tt> on the instance <tt>ingredients</tt> by connecting it with a ".", as we have seen in the lesson about object-oriented programming.  The <i>argument</i> of <tt>append</tt> is the object we want to add to the list.  It is always appended at the end of the list. </p>

<p>Lists have many other methods; I will introduce them when needed.  If you want to see them all, just type <tt>dir([])</tt> at the Python interpreter; note that <tt>[]</tt> is an empty list - any list would give the same result here.  See if you can spot the method name <tt>append</tt>.</p>

<p>Now that we know how to add items to lists, we need to know how to remove them.  To do this, we need to use the Python keyword <span class="pykeyword">del</span>.</p>

<pre>
>>> <span class="keyword">del</span> ingredients[3]
>>> <span class="keyword">print</span> ingredients
[<span class="string">'4 eggs'</span>, <span class="string">'100 g of sugar'</span>, <span class="string">'250 g of dark semi-sweet chocolate'</span>, <span class="string">'whipped cream'</span>]
</pre>

<p>The Python keyword <span class="pykeyword">del</span> can do much more than removing items in a list.  For example, it can make Python forget about a variable that had been defined.</p>

<pre>
>>> a = 3
>>> <span class="keyword">print</span> a
3
>>> <span class="keyword">del</span> a
>>> <span class="keyword">print</span> a
Traceback (most recent call last):
  File <span class="string">"&lt;input>"</span>, line 1, in ?
NameError: name <span class="string">'a'</span> is not defined
</pre>

<!--==============================-->
<hr class="line" />

<h3 class="try">Try it!</h3>

<p>In Reeborg's world, write a simple program that creates two robots, each with its own name, have them take a few steps and try to make one of them disappear using the keyword <span class="pykeyword">del</span>.  You may need to have the remaining robot take at least one more step (before turning it off) to have the other robot disappear from the screen.</p>

<!--==============================-->
<hr class="line" />

<h3 class="section">A word about <tt>range()</tt></h3>

<p>You are now ready to learn a secret about the Python function <tt>range()</tt>:</p>

<pre>
>>> <span class="keyword">print</span> range(6)
[0, 1, 2, 3, 4, 5]
</pre>

<p>That's right: <tt>range()</tt> simply creates a list!</p>

<!--==============================-->
<hr class="line" />

<h3 class="section">Converting between lists, integers and strings</h3>

<p>Here are a few useful built-in functions to help converting between one <i>data type</i> and another.</p>

<pre>
>>>  first_number = 1304
>>>  second_number = 987
>>>  
>>>  <span class="comment"># converting an integer to a string using str()</span>
>>>  first_string = str(first_number)
>>>  first_string
'1304'
>>>  second_string = str(second_number)
>>>  second_string
'987'

>>>  <span class="comment"># converting a string to a number using int()</span>
>>>  int(first_string)
1304

>>>  <span class="comment"># converting a string to a list using list()</span>
>>>  first_list = list(first_string)
>>>  first_list
['1', '3', '0', '4']

>>>  second_list = list(second_string)
>>>  second_list
['9', '8', '7']

>>>  <span class="comment"># using join() to create a string from a list of strings</span>
>>>  '...'.join(second_string)
'9...8...7'
>>>  '-'.join(first_string)
'1-3-0-4'
>>>  <span class="comment"># use the empty string '' to join the elements...</span>
>>>  ''.join(first_string)
>>>  '1304'
>>>  ''.join(second_string)
>>>  '987'

>>>  <span class="comment"># extracting the last element of a list with pop()</span>
>>>  <span class="comment"># Note: pop() is, in a way, the opposite of append() </span>
>>>  first_list
['1', '3', '0', '4']
>>>  last_element = first_list.pop()
>>>  last_element
'4'
>>>  first_list
['1', '3', '0']

>>>  <span class="comment"># the function len() gives the number </span>
>>>  <span class="comment"># of items in a list </span>
>>>  len(first_list)
3
</pre>

<!--==============================-->
<hr class="line" />

<h3 class="try">Help Reeborg to add using lists, strings and integers</h3>

<p>Help Reeborg to add two numbers, like illustrated below, using what we just saw about lists, strings and integers.</p>

<p><img alt="before addition" src=
"../../images/inter/add34_start.png" /></p>

<p><img alt="after addition" src=
"../../images/inter/add34_end.png" /></p>

<div class="lessons_nav">
<a href="33-for.htm"><img alt="previous" src=
"../../images/previous.png" />Repeat()is hiding some Python keywords</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png" /></a> - <a href=
"35-dicts.htm">An other kind of definition <img alt="next"
src="../../images/next.png" /></a>
</div>
</body>
</html>
